<?php

declare (strict_types=1);

namespace app\services\v1\recovery;

use app\dao\v1\recovery\RecoveryProductDao;
use app\model\v1\recovery\RecoveryProduct;
use app\model\v1\recovery\RecoveryProductShop;
use app\services\BaseServices;
use app\services\v1\recovery\RecoveryUserSearchHistoryServices;
use crmeb\traits\ServicesTrait;

class RecoveryProductServices extends BaseServices
{

    use ServicesTrait;

    public function __construct(RecoveryProductDao $dao)
    {
        $this->dao = $dao;
    }


    /**
     * 首页 - top区域 - 搜索
     * @param int $uid
     * @param string $field
     * @param string $word
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTopSearchList(int $uid, string $field = '*', string $word = '', int $type=1)
    {
        if($type == 1) {
            /*
            $where = $this->getSearchListWhere($word, $uid);

            [$page, $limit] = $this->getPageValue();
            $page = $page > 0 ? $page : 1;
            $limit = $limit > 0 ? $limit : 10;
            //限制一次最多请求条数
            if ($limit > 10) $limit = 10;

            $order = 'id desc';
            $list["data"] = $this->dao->getList($where, $field, $page, $limit, $order);
            $list["count"] = $this->dao->getListCount($where, $field);
            return $list;
            */

            $whereParam["word"] = $word;
            $list = RecoveryProduct::getProductLists($whereParam);
            return $list;
        }

        if(in_array($type, [2,3])) {
            if ($type == 2) $whereType = 1;
            if ($type == 3) $whereType = 2;

            $whereParam["type"] = $whereType;
            $whereParam["word"] = $word;
            $whereParam["sys_word"] = 1;
            $list = RecoveryProductShop::getProductShopList($whereParam);
            return $list;
        }
        return [];
    }



    public function getSearchListWhere(string $word = '', int $uid)
    {
        $where = [];
        $where['status'] = 1;
        //根据商品名称搜索
        if(!empty($word)) {
            $where['word'] = $word;

            //添加一条用户搜索
            /** @var RecoveryUserSearchHistoryServices $recoveryUserSearchHistoryServices */
            $recoveryUserSearchHistoryServices = app()->make(RecoveryUserSearchHistoryServices::class);
            $recoveryUserSearchHistoryServices->createOne(["uid" => $uid, "keyword" => $word]);
        }
        return $where;
    }










}